home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 3 / Gold Medal Software - Volume 3 (Gold Medal) (1994).iso / prog / dflt18.arj / NORMAL.C < prev    next >
Text File  |  1994-03-07  |  33KB  |  1,104 lines

  1. /* ------------- normal.c ------------ */
  2.  
  3. #include "dflat.h"
  4.  
  5. #ifdef INCLUDE_MULTI_WINDOWS
  6. static void near PaintOverLappers(WINDOW wnd);
  7. static void near PaintUnderLappers(WINDOW wnd);
  8. #endif
  9.  
  10. static BOOL InsideWindow(WINDOW, int, int);
  11. static void TerminateMoveSize(void);
  12. static void SaveBorder(RECT);
  13. static void RestoreBorder(RECT);
  14. static void GetVideoBuffer(WINDOW);
  15. static void PutVideoBuffer(WINDOW);
  16. #ifdef INCLUDE_MINIMIZE
  17. static RECT PositionIcon(WINDOW);
  18. #endif
  19. static void near dragborder(WINDOW, int, int);
  20. static void near sizeborder(WINDOW, int, int);
  21. static int px = -1, py = -1;
  22. static int diff;
  23. static struct window dwnd = {DUMMY, NULL, NormalProc,
  24.                                 {-1,-1,-1,-1}};
  25. static int *Bsave;
  26. static int Bht, Bwd;
  27. BOOL WindowMoving;
  28. BOOL WindowSizing;
  29. /* -------- array of class definitions -------- */
  30. CLASSDEFS classdefs[] = {
  31.     #undef ClassDef
  32.     #define ClassDef(c,b,p,a) {b,p,a},
  33.     #include "classes.h"
  34. };
  35. WINDOW HiddenWindow;
  36.  
  37. /* --------- CREATE_WINDOW Message ---------- */
  38. static void CreateWindowMsg(WINDOW wnd)
  39. {
  40.     AppendWindow(wnd);
  41.     if (!SendMessage(NULL, MOUSE_INSTALLED, 0, 0))
  42.         ClearAttribute(wnd, VSCROLLBAR | HSCROLLBAR);
  43.     if (TestAttribute(wnd, SAVESELF) && isVisible(wnd))
  44.         GetVideoBuffer(wnd);
  45. }
  46.  
  47. /* --------- SHOW_WINDOW Message ---------- */
  48. static void ShowWindowMsg(WINDOW wnd, PARAM p1, PARAM p2)
  49. {
  50.     if (GetParent(wnd) == NULL || isVisible(GetParent(wnd)))    {
  51.         WINDOW cwnd;
  52.         if (TestAttribute(wnd, SAVESELF) &&
  53.                         wnd->videosave == NULL)
  54.             GetVideoBuffer(wnd);
  55.         SetVisible(wnd);
  56.         SendMessage(wnd, PAINT, 0, TRUE);
  57.         SendMessage(wnd, BORDER, 0, 0);
  58.         /* --- show the children of this window --- */
  59.         cwnd = FirstWindow(wnd);
  60.         while (cwnd != NULL)    {
  61.             if (cwnd->condition != ISCLOSING)
  62.                 SendMessage(cwnd, SHOW_WINDOW, p1, p2);
  63.             cwnd = NextWindow(cwnd);
  64.         }
  65.     }
  66. }
  67.  
  68. /* --------- HIDE_WINDOW Message ---------- */
  69. static void HideWindowMsg(WINDOW wnd)
  70. {
  71.     if (isVisible(wnd))    {
  72.         ClearVisible(wnd);
  73.         /* --- paint what this window covered --- */
  74.         if (TestAttribute(wnd, SAVESELF))
  75.             PutVideoBuffer(wnd);
  76. #ifdef INCLUDE_MULTI_WINDOWS
  77.         else
  78.             PaintOverLappers(wnd);
  79. #endif
  80.     }
  81. }
  82.  
  83. /* --------- KEYBOARD Message ---------- */
  84. static BOOL KeyboardMsg(WINDOW wnd, PARAM p1, PARAM p2)
  85. {
  86.     if (WindowMoving || WindowSizing)    {
  87.         /* -- move or size a window with keyboard -- */
  88.         int x, y;
  89.         x=WindowMoving?GetLeft(&dwnd):GetRight(&dwnd);
  90.         y=WindowMoving?GetTop(&dwnd):GetBottom(&dwnd);
  91.         switch ((int)p1)    {
  92.             case ESC:
  93.                 TerminateMoveSize();
  94.                 return TRUE;
  95.             case UP:
  96.                 if (y)
  97.                     --y;
  98.                 break;
  99.             case DN:
  100.                 if (y < SCREENHEIGHT-1)
  101.                     y++;
  102.                 break;
  103.             case FWD:
  104.                 if (x < SCREENWIDTH-1)
  105.                     x++;
  106.                 break;
  107.             case BS:
  108.                 if (x)
  109.                     --x;
  110.                 break;
  111.             case '\r':
  112.                 SendMessage(wnd,BUTTON_RELEASED,x,y);
  113.             default:
  114.                 return TRUE;
  115.         }
  116.         /* -- use the mouse functions to move/size - */
  117.         SendMessage(wnd, MOUSE_CURSOR, x, y);
  118.         SendMessage(wnd, MOUSE_MOVED, x, y);
  119.         return TRUE;
  120.     }
  121.     switch ((int)p1)    {
  122.         case F1:
  123.             SendMessage(wnd, COMMAND, ID_HELP, 0);
  124.             return TRUE;
  125.         case ' ':
  126.             if ((int)p2 & ALTKEY)
  127.                 if (TestAttribute(wnd, HASTITLEBAR))
  128.                     if (TestAttribute(wnd, CONTROLBOX))
  129.                         BuildSystemMenu(wnd);
  130.             return TRUE;
  131.         case CTRL_F4:
  132.             if (TestAttribute(wnd, CONTROLBOX))    {
  133.                 SendMessage(wnd, CLOSE_WINDOW, 0, 0);
  134.                 SkipApplicationControls();
  135.                 return TRUE;
  136.             }
  137.             break;
  138.         default:
  139.             break;
  140.     }
  141.     return FALSE;
  142. }
  143.  
  144. /* --------- COMMAND Message ---------- */
  145. static void CommandMsg(WINDOW wnd, PARAM p1)
  146. {
  147.     switch ((int)p1)    {
  148.         case ID_HELP:
  149.             DisplayHelp(wnd,ClassNames[GetClass(wnd)]);
  150.             break;
  151. #ifdef INCLUDE_RESTORE
  152.         case ID_SYSRESTORE:
  153.             SendMessage(wnd, RESTORE, 0, 0);
  154.             break;
  155. #endif
  156.         case ID_SYSMOVE:
  157.             SendMessage(wnd, CAPTURE_MOUSE, TRUE,
  158.                 (PARAM) &dwnd);
  159.             SendMessage(wnd, CAPTURE_KEYBOARD, TRUE,
  160.                 (PARAM) &dwnd);
  161.             SendMessage(wnd, MOUSE_CURSOR,
  162.                 GetLeft(wnd), GetTop(wnd));
  163.             WindowMoving = TRUE;
  164.             dragborder(wnd, GetLeft(wnd), GetTop(wnd));
  165.             break;
  166.         case ID_SYSSIZE:
  167.             SendMessage(wnd, CAPTURE_MOUSE, TRUE,
  168.                 (PARAM) &dwnd);
  169.             SendMessage(wnd, CAPTURE_KEYBOARD, TRUE,
  170.                 (PARAM) &dwnd);
  171.             SendMessage(wnd, MOUSE_CURSOR,
  172.                 GetRight(wnd), GetBottom(wnd));
  173.             WindowSizing = TRUE;
  174.             dragborder(wnd, GetLeft(wnd), GetTop(wnd));
  175.             break;
  176. #ifdef INCLUDE_MINIMIZE
  177.         case ID_SYSMINIMIZE:
  178.             SendMessage(wnd, MINIMIZE, 0, 0);
  179.             break;
  180. #endif
  181. #ifdef INCLUDE_MAXIMIZE
  182.         case ID_SYSMAXIMIZE:
  183.             SendMessage(wnd, MAXIMIZE, 0, 0);
  184.             break;
  185. #endif
  186.         case ID_SYSCLOSE:
  187.             SendMessage(wnd, CLOSE_WINDOW, 0, 0);
  188.             SkipApplicationControls();
  189.             break;
  190.         default:
  191.             break;
  192.     }
  193. }
  194.  
  195. /* --------- SETFOCUS Message ---------- */
  196. static void SetFocusMsg(WINDOW wnd, PARAM p1)
  197. {
  198.     RECT rc = {0,0,0,0};
  199.     if (p1 && wnd != NULL && inFocus != wnd)    {
  200.         WINDOW this, thispar;
  201.         WINDOW that = NULL, thatpar = NULL;
  202.  
  203.         WINDOW cwnd = wnd, fwnd = GetParent(wnd);
  204.         /* ---- post focus in ancestors ---- */
  205.         while (fwnd != NULL)    {
  206.             fwnd->childfocus = cwnd;
  207.             cwnd = fwnd;
  208.             fwnd = GetParent(fwnd);
  209.         }
  210.         /* ---- de-post focus in self and children ---- */
  211.         fwnd = wnd;
  212.         while (fwnd != NULL)    {
  213.             cwnd = fwnd->childfocus;
  214.             fwnd->childfocus = NULL;
  215.             fwnd = cwnd;
  216.         }
  217.  
  218.         this = wnd;
  219.         that = thatpar = inFocus;
  220.  
  221.         /* ---- find common ancestor of prev focus and this window --- */
  222.         while (thatpar != NULL)    {
  223.             thispar = wnd;
  224.             while (thispar != NULL)    {
  225.                 if (this == CaptureMouse || this == CaptureKeyboard)    {
  226.                     /* ---- don't repaint if this window has capture ---- */
  227.                     that = thatpar = NULL;
  228.                     break;
  229.                 }
  230.                 if (thispar == thatpar)    {
  231.                     /* ---- don't repaint if SAVESELF window had focus ---- */
  232.                     if (this != that && TestAttribute(that, SAVESELF))
  233.                         that = thatpar = NULL;
  234.                     break;
  235.                 }
  236.                 this = thispar;
  237.                 thispar = GetParent(thispar);
  238.             }
  239.             if (thispar != NULL)
  240.                 break;
  241.             that = thatpar;
  242.             thatpar = GetParent(thatpar);
  243.         }
  244.         if (inFocus != NULL)
  245.             SendMessage(inFocus, SETFOCUS, FALSE, 0);
  246.         inFocus = wnd;
  247.         if (that != NULL && isVisible(wnd))    {
  248.             rc = subRectangle(WindowRect(that), WindowRect(this));
  249.             if (!ValidRect(rc))    {
  250.                 if (ApplicationWindow != NULL)    {
  251.                     WINDOW fwnd = FirstWindow(ApplicationWindow);
  252.                     while (fwnd != NULL)    {
  253.                         if (!isAncestor(wnd, fwnd))    {
  254.                             rc = subRectangle(WindowRect(wnd),WindowRect(fwnd));
  255.                             if (ValidRect(rc))
  256.                                 break;
  257.                         }
  258.                         fwnd = NextWindow(fwnd);
  259.                     }
  260.                 }
  261.             }
  262.         }
  263.         if (that != NULL && !ValidRect(rc) && isVisible(wnd))
  264.             this = NULL;
  265.         ReFocus(wnd);
  266.         if (this != NULL && !TestAttribute(this, SAVESELF))
  267.             SendMessage(this, SHOW_WINDOW, 0, 0);
  268.         else 
  269.             SendMessage(wnd, BORDER, 0, 0);
  270.     }
  271.     else if (!p1 && inFocus == wnd)    {
  272.         /* -------- clearing focus --------- */
  273.         inFocus = NULL;
  274.         SendMessage(wnd, BORDER, 0, 0);
  275.     }
  276. }
  277.  
  278. /* --------- DOUBLE_CLICK Message ---------- */
  279. static void DoubleClickMsg(WINDOW wnd, PARAM p1, PARAM p2)
  280. {
  281.     int mx = (int) p1 - GetLeft(wnd);
  282.     int my = (int) p2 - GetTop(wnd);
  283.     if (!WindowSizing && !WindowMoving)    {
  284.         if (HitControlBox(wnd, mx, my))    {
  285.             PostMessage(wnd, CLOSE_WINDOW, 0, 0);
  286.             SkipApplicationControls();
  287.         }
  288.     }
  289. }
  290.  
  291. /* --------- LEFT_BUTTON Message ---------- */
  292. static void LeftButtonMsg(WINDOW wnd, PARAM p1, PARAM p2)
  293. {
  294.     int mx = (int) p1 - GetLeft(wnd);
  295.     int my = (int) p2 - GetTop(wnd);
  296.     if (WindowSizing || WindowMoving)
  297.         return;
  298.     if (HitControlBox(wnd, mx, my))    {
  299.         BuildSystemMenu(wnd);
  300.         return;
  301.     }
  302.     if (my == 0 && mx > -1 && mx < WindowWidth(wnd))  {
  303.         /* ---------- hit the top border -------- */
  304.         if (TestAttribute(wnd, MINMAXBOX) &&
  305.                 TestAttribute(wnd, HASTITLEBAR))  {
  306.             if (mx == WindowWidth(wnd)-2)    {
  307.                 if (wnd->condition != ISRESTORED)
  308.                     /* --- hit the restore box --- */
  309.                     SendMessage(wnd, RESTORE, 0, 0);
  310. #ifdef INCLUDE_MAXIMIZE
  311.                 else
  312.                     /* --- hit the maximize box --- */
  313.                     SendMessage(wnd, MAXIMIZE, 0, 0);
  314. #endif
  315.                 return;
  316.             }
  317. #ifdef INCLUDE_MINIMIZE
  318.             if (mx == WindowWidth(wnd)-3)    {
  319.                 /* --- hit the minimize box --- */
  320.                 if (wnd->condition != ISMINIMIZED)
  321.                     SendMessage(wnd, MINIMIZE, 0, 0);
  322.                 return;
  323.             }
  324. #endif
  325.         }
  326. #ifdef INCLUDE_MAXIMIZE
  327.         if (wnd->condition == ISMAXIMIZED)
  328.             return;
  329. #endif
  330.         if (TestAttribute(wnd, MOVEABLE))    {
  331.             WindowMoving = TRUE;
  332.             px = mx;
  333.             py = my;
  334.             diff = (int) mx;
  335.             SendMessage(wnd, CAPTURE_MOUSE, TRUE,
  336.                 (PARAM) &dwnd);
  337.             dragborder(wnd, GetLeft(wnd), GetTop(wnd));
  338.         }
  339.         return;
  340.     }
  341.     if (mx == WindowWidth(wnd)-1 &&
  342.             my == WindowHeight(wnd)-1)    {
  343.         /* ------- hit the resize corner ------- */
  344. #ifdef INCLUDE_MINIMIZE
  345.         if (wnd->condition == ISMINIMIZED)
  346.             return;
  347. #endif
  348.         if (!TestAttribute(wnd, SIZEABLE))
  349.             return;
  350. #ifdef INCLUDE_MAXIMIZE
  351.         if (wnd->condition == ISMAXIMIZED)    {
  352.             if (GetParent(wnd) == NULL)
  353.                 return;
  354.             if (TestAttribute(GetParent(wnd),HASBORDER))
  355.                 return;
  356.             /* ----- resizing a maximized window over a
  357.                     borderless parent ----- */
  358.             wnd = GetParent(wnd);
  359.         }
  360. #endif
  361.         WindowSizing = TRUE;
  362.         SendMessage(wnd, CAPTURE_MOUSE,
  363.             TRUE, (PARAM) &dwnd);
  364.         dragborder(wnd, GetLeft(wnd), GetTop(wnd));
  365.     }
  366. }
  367.  
  368. /* --------- MOUSE_MOVED Message ---------- */
  369. static BOOL MouseMovedMsg(WINDOW wnd, PARAM p1, PARAM p2)
  370. {
  371.     if (WindowMoving)    {
  372.         int leftmost = 0, topmost = 0,
  373.             bottommost = SCREENHEIGHT-2,
  374.             rightmost = SCREENWIDTH-2;
  375.         int x = (int) p1 - diff;
  376.         int y = (int) p2;
  377.         if (GetParent(wnd) != NULL &&
  378.                 !TestAttribute(wnd, NOCLIP))    {
  379.             WINDOW wnd1 = GetParent(wnd);
  380.             topmost    = GetClientTop(wnd1);
  381.             leftmost   = GetClientLeft(wnd1);
  382.             bottommost = GetClientBottom(wnd1);
  383.             rightmost  = GetClientRight(wnd1);
  384.         }
  385.         if (x < leftmost || x > rightmost ||
  386.                 y < topmost || y > bottommost)    {
  387.             x = max(x, leftmost);
  388.             x = min(x, rightmost);
  389.             y = max(y, topmost);
  390.             y = min(y, bottommost);
  391.             SendMessage(NULL,MOUSE_CURSOR,x+diff,y);
  392.         }
  393.         if (x != px || y != py)    {
  394.             px = x;
  395.             py = y;
  396.             dragborder(wnd, x, y);
  397.         }
  398.         return TRUE;
  399.     }
  400.     if (WindowSizing)    {
  401.         sizeborder(wnd, (int) p1, (int) p2);
  402.         return TRUE;
  403.     }
  404.     return FALSE;
  405. }
  406.  
  407. #ifdef INCLUDE_MAXIMIZE
  408. /* --------- MAXIMIZE Message ---------- */
  409. static void MaximizeMsg(WINDOW wnd)
  410. {
  411.     RECT rc = {0, 0, 0, 0};
  412.     RECT holdrc;
  413.     holdrc = wnd->RestoredRC;
  414.     rc.rt = SCREENWIDTH-1;
  415.     rc.bt = SCREENHEIGHT-1;
  416.     if (GetParent(wnd))
  417.         rc = ClientRect(GetParent(wnd));
  418.     wnd->oldcondition = wnd->condition;
  419.     wnd->condition = ISMAXIMIZED;
  420.     wnd->wasCleared = FALSE;
  421.     SendMessage(wnd, HIDE_WINDOW, 0, 0);
  422.     SendMessage(wnd, MOVE,
  423.         RectLeft(rc), RectTop(rc));
  424.     SendMessage(wnd, SIZE,
  425.         RectRight(rc), RectBottom(rc));
  426.     if (wnd->restored_attrib == 0)
  427.         wnd->restored_attrib = wnd->attrib;
  428.     ClearAttribute(wnd, SHADOW);
  429.     SendMessage(wnd, SHOW_WINDOW, 0, 0);
  430.     wnd->RestoredRC = holdrc;
  431. }
  432. #endif
  433.  
  434. #ifdef INCLUDE_MINIMIZE
  435. /* --------- MINIMIZE Message ---------- */
  436. static void MinimizeMsg(WINDOW wnd)
  437. {
  438.     RECT rc;
  439.     RECT holdrc;
  440.  
  441.     holdrc = wnd->RestoredRC;
  442.     rc = PositionIcon(wnd);
  443.     wnd->oldcondition = wnd->condition;
  444.     wnd->condition = ISMINIMIZED;
  445.     wnd->wasCleared = FALSE;
  446.     SendMessage(wnd, HIDE_WINDOW, 0, 0);
  447.     SendMessage(wnd, MOVE,
  448.         RectLeft(rc), RectTop(rc));
  449.     SendMessage(wnd, SIZE,
  450.         RectRight(rc), RectBottom(rc));
  451.     if (wnd == inFocus)
  452.         SetNextFocus();
  453.     if (wnd->restored_attrib == 0)
  454.         wnd->restored_attrib = wnd->attrib;
  455.     ClearAttribute(wnd,
  456.         SHADOW | SIZEABLE | HASMENUBAR |
  457.         VSCROLLBAR | HSCROLLBAR);
  458.     SendMessage(wnd, SHOW_WINDOW, 0, 0);
  459.     wnd->RestoredRC = holdrc;
  460. }
  461. #endif
  462.  
  463. #ifdef INCLUDE_RESTORE
  464. /* --------- RESTORE Message ---------- */
  465. static void RestoreMsg(WINDOW wnd)
  466. {
  467.     RECT holdrc;
  468.     holdrc = wnd->RestoredRC;
  469.     wnd->oldcondition = wnd->condition;
  470.     wnd->condition = ISRESTORED;
  471.     wnd->wasCleared = FALSE;
  472.     SendMessage(wnd, HIDE_WINDOW, 0, 0);
  473.     wnd->attrib = wnd->restored_attrib;
  474.     wnd->restored_attrib = 0;
  475.     SendMessage(wnd, MOVE, wnd->RestoredRC.lf,
  476.         wnd->RestoredRC.tp);
  477.     wnd->RestoredRC = holdrc;
  478.     SendMessage(wnd, SIZE, wnd->RestoredRC.rt,
  479.         wnd->RestoredRC.bt);
  480.     if (wnd != inFocus)
  481.         SendMessage(wnd, SETFOCUS, TRUE, 0);
  482.     else
  483.         SendMessage(wnd, SHOW_WINDOW, 0, 0);
  484. }
  485. #endif
  486.  
  487. /* --------- MOVE Message ---------- */
  488. static void MoveMsg(WINDOW wnd, PARAM p1, PARAM p2)
  489. {
  490.     WINDOW cwnd;
  491.     BOOL wasVisible = isVisible(wnd);
  492.     int xdif = (int) p1 - wnd->rc.lf;
  493.     int ydif = (int) p2 - wnd->rc.tp;
  494.  
  495.     if (xdif == 0 && ydif == 0)
  496.         return;
  497.     wnd->wasCleared = FALSE;
  498.     if (wasVisible)
  499.         SendMessage(wnd, HIDE_WINDOW, 0, 0);
  500.     wnd->rc.lf = (int) p1;
  501.     wnd->rc.tp = (int) p2;
  502.     wnd->rc.rt = GetLeft(wnd)+WindowWidth(wnd)-1;
  503.     wnd->rc.bt = GetTop(wnd)+WindowHeight(wnd)-1;
  504.     if (wnd->condition == ISRESTORED)
  505.         wnd->RestoredRC = wnd->rc;
  506.  
  507.     cwnd = FirstWindow(wnd);
  508.     while (cwnd != NULL)    {
  509.         SendMessage(cwnd, MOVE, cwnd->rc.lf+xdif, cwnd->rc.tp+ydif);
  510.         cwnd = NextWindow(cwnd);
  511.     }
  512.     if (wasVisible)
  513.         SendMessage(wnd, SHOW_WINDOW, 0, 0);
  514. }
  515.  
  516. /* --------- SIZE Message ---------- */
  517. static void SizeMsg(WINDOW wnd, PARAM p1, PARAM p2)
  518. {
  519.     BOOL wasVisible = isVisible(wnd);
  520.     WINDOW cwnd;
  521.     RECT rc;
  522.     int xdif = (int) p1 - wnd->rc.rt;
  523.     int ydif = (int) p2 - wnd->rc.bt;
  524.  
  525.     if (xdif == 0 && ydif == 0)
  526.         return;
  527.     wnd->wasCleared = FALSE;
  528.     if (wasVisible)
  529.         SendMessage(wnd, HIDE_WINDOW, 0, 0);
  530.     wnd->rc.rt = (int) p1;
  531.     wnd->rc.bt = (int) p2;
  532.     wnd->ht = GetBottom(wnd)-GetTop(wnd)+1;
  533.     wnd->wd = GetRight(wnd)-GetLeft(wnd)+1;
  534.  
  535.     if (wnd->condition == ISRESTORED)
  536.         wnd->RestoredRC = WindowRect(wnd);
  537.  
  538. #ifdef INCLUDE_MAXIMIZE
  539.     rc = ClientRect(wnd);
  540.  
  541.     cwnd = FirstWindow(wnd);
  542.     while (cwnd != NULL)    {
  543.         if (cwnd->condition == ISMAXIMIZED)
  544.             SendMessage(cwnd, SIZE, RectRight(rc), RectBottom(rc));
  545.         cwnd = NextWindow(cwnd);
  546.     }
  547.  
  548. #endif
  549.     if (wasVisible)
  550.         SendMessage(wnd, SHOW_WINDOW, 0, 0);
  551. }
  552.  
  553. /* --------- CLOSE_WINDOW Message ---------- */
  554. static void CloseWindowMsg(WINDOW wnd)
  555. {
  556.     WINDOW cwnd;
  557.     wnd->condition = ISCLOSING;
  558.     if (wnd->PrevMouse != NULL)
  559.         SendMessage(wnd, RELEASE_MOUSE, 0, 0);
  560.     if (wnd->PrevKeyboard != NULL)
  561.         SendMessage(wnd, RELEASE_KEYBOARD, 0, 0);
  562.     /* ----------- hide this window ------------ */
  563.     SendMessage(wnd, HIDE_WINDOW, 0, 0);
  564.     /* --- close the children of this window --- */
  565.  
  566.     cwnd = LastWindow(wnd);
  567.     while (cwnd != NULL)    {
  568.         if (inFocus == cwnd)
  569.             inFocus = wnd;
  570.         SendMessage(cwnd,CLOSE_WINDOW,0,0);
  571.         cwnd = LastWindow(wnd);
  572.     }
  573.  
  574.     /* --- change focus if this window had it -- */
  575.     if (wnd == inFocus)
  576.         SetPrevFocus();
  577.     /* -- free memory allocated to this window - */
  578.     if (wnd->title != NULL)
  579.         free(wnd->title);
  580.     if (wnd->videosave != NULL)
  581.         free(wnd->videosave);
  582.     /* -- remove window from parent's list of children -- */
  583.     RemoveWindow(wnd);
  584.     if (wnd == inFocus)
  585.         inFocus = NULL;
  586.     free(wnd);
  587. }
  588.  
  589. /* ---- Window-processing module for NORMAL window class ---- */
  590. int NormalProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  591. {
  592.     switch (msg)    {
  593.         case CREATE_WINDOW:
  594.             CreateWindowMsg(wnd);
  595.             break;
  596.         case SHOW_WINDOW:
  597.             ShowWindowMsg(wnd, p1, p2);
  598.             break;
  599.         case HIDE_WINDOW:
  600.             HideWindowMsg(wnd);
  601.             break;
  602.         case DISPLAY_HELP:
  603.             DisplayHelp(wnd, (char *)p1);
  604.             break;
  605.         case INSIDE_WINDOW:
  606.             return InsideWindow(wnd, (int) p1, (int) p2);
  607.         case KEYBOARD:
  608.             if (KeyboardMsg(wnd, p1, p2))
  609.                 return TRUE;
  610.             /* ------- fall through ------- */
  611.         case ADDSTATUS:
  612.         case SHIFT_CHANGED:
  613.             if (GetParent(wnd) != NULL)
  614.                 PostMessage(GetParent(wnd), msg, p1, p2);
  615.             break;
  616.         case PAINT:
  617.             if (isVisible(wnd))    {
  618. #ifdef INCLUDE_MULTI_WINDOWS
  619.                 if (wnd->wasCleared)
  620.                     PaintUnderLappers(wnd);
  621.                 else
  622. #endif
  623.                 {
  624.                     wnd->wasCleared = TRUE;
  625.                     ClearWindow(wnd, (RECT *)p1, ' ');
  626.                 }
  627.             }
  628.             break;
  629.         case BORDER:
  630.             if (isVisible(wnd))    {
  631.                 if (TestAttribute(wnd, HASBORDER))
  632.                     RepaintBorder(wnd, (RECT *)p1);
  633.                 else if (TestAttribute(wnd, HASTITLEBAR))
  634.                     DisplayTitle(wnd, (RECT *)p1);
  635.             }
  636.             break;
  637.         case COMMAND:
  638.             CommandMsg(wnd, p1);
  639.             break;
  640.         case SETFOCUS:
  641.             SetFocusMsg(wnd, p1);
  642.             break;
  643.         case DOUBLE_CLICK:
  644.             DoubleClickMsg(wnd, p1, p2);
  645.             break;
  646.         case LEFT_BUTTON:
  647.             LeftButtonMsg(wnd, p1, p2);
  648.             break;
  649.         case MOUSE_MOVED:
  650.             if (MouseMovedMsg(wnd, p1, p2))
  651.                 return TRUE;
  652.             break;
  653.         case BUTTON_RELEASED:
  654.             if (WindowMoving || WindowSizing)    {
  655.                 if (WindowMoving)
  656.                     PostMessage(wnd,MOVE,dwnd.rc.lf,dwnd.rc.tp);
  657.                 else
  658.                     PostMessage(wnd,SIZE,dwnd.rc.rt,dwnd.rc.bt);
  659.                 TerminateMoveSize();
  660.             }
  661.             break;
  662. #ifdef INCLUDE_MAXIMIZE
  663.         case MAXIMIZE:
  664.             if (wnd->condition != ISMAXIMIZED)
  665.                 MaximizeMsg(wnd);
  666.             break;
  667. #endif
  668. #ifdef INCLUDE_MINIMIZE
  669.         case MINIMIZE:
  670.             if (wnd->condition != ISMINIMIZED)
  671.                 MinimizeMsg(wnd);
  672.             break;
  673. #endif
  674. #ifdef INCLUDE_RESTORE
  675.         case RESTORE:
  676.             if (wnd->condition != ISRESTORED)    {
  677. #ifdef INCLUDE_MAXIMIZE
  678.                 if (wnd->oldcondition == ISMAXIMIZED)
  679.                     SendMessage(wnd, MAXIMIZE, 0, 0);
  680.                 else
  681. #endif
  682.                     RestoreMsg(wnd);
  683.             }
  684.             break;
  685. #endif
  686.         case MOVE:
  687.             MoveMsg(wnd, p1, p2);
  688.             break;
  689.         case SIZE:    {
  690.             SizeMsg(wnd, p1, p2);
  691.             break;
  692.         }
  693.         case CLOSE_WINDOW:
  694.             CloseWindowMsg(wnd);
  695.             break;
  696.         default:
  697.             break;
  698.     }
  699.     return TRUE;
  700. }
  701. #ifdef INCLUDE_MINIMIZE
  702. /* ---- compute lower right icon space in a rectangle ---- */
  703. static RECT LowerRight(RECT prc)
  704. {
  705.     RECT rc;
  706.     RectLeft(rc) = RectRight(prc) - ICONWIDTH;
  707.     RectTop(rc) = RectBottom(prc) - ICONHEIGHT;
  708.     RectRight(rc) = RectLeft(rc)+ICONWIDTH-1;
  709.     RectBottom(rc) = RectTop(rc)+ICONHEIGHT-1;
  710.     return rc;
  711. }
  712. /* ----- compute a position for a minimized window icon ---- */
  713. static RECT PositionIcon(WINDOW wnd)
  714. {
  715.     WINDOW pwnd = GetParent(wnd);
  716.     RECT rc;
  717.     RectLeft(rc) = SCREENWIDTH-ICONWIDTH;
  718.     RectTop(rc) = SCREENHEIGHT-ICONHEIGHT;
  719.     RectRight(rc) = SCREENWIDTH-1;
  720.     RectBottom(rc) = SCREENHEIGHT-1;
  721.     if (pwnd != NULL)    {
  722.         RECT prc = WindowRect(pwnd);
  723.         WINDOW cwnd = FirstWindow(pwnd);
  724.         rc = LowerRight(prc);
  725.         /* - search for icon available location - */
  726.         while (cwnd != NULL)    {
  727.             if (cwnd->condition == ISMINIMIZED)    {
  728.                 RECT rc1;
  729.                 rc1 = WindowRect(cwnd);
  730.                 if (RectLeft(rc1) == RectLeft(rc) &&
  731.                         RectTop(rc1) == RectTop(rc))    {
  732.                     RectLeft(rc) -= ICONWIDTH;
  733.                     RectRight(rc) -= ICONWIDTH;
  734.                     if (RectLeft(rc) < RectLeft(prc)+1)   {
  735.                         RectLeft(rc) =
  736.                             RectRight(prc)-ICONWIDTH;
  737.                         RectRight(rc) =
  738.                             RectLeft(rc)+ICONWIDTH-1;
  739.                         RectTop(rc) -= ICONHEIGHT;
  740.                         RectBottom(rc) -= ICONHEIGHT;
  741.                         if (RectTop(rc) < RectTop(prc)+1)
  742.                             return LowerRight(prc);
  743.                     }
  744.                     break;
  745.                 }
  746.             }
  747.             cwnd = NextWindow(cwnd);
  748.         }
  749.     }
  750.     return rc;
  751. }
  752. #endif
  753. /* ----- terminate the move or size operation ----- */
  754. static void TerminateMoveSize(void)
  755. {
  756.     px = py = -1;
  757.     diff = 0;
  758.     SendMessage(&dwnd, RELEASE_MOUSE, TRUE, 0);
  759.     SendMessage(&dwnd, RELEASE_KEYBOARD, TRUE, 0);
  760.     RestoreBorder(dwnd.rc);
  761.     WindowMoving = WindowSizing = FALSE;
  762. }
  763. /* ---- build a dummy window border for moving or sizing --- */
  764. static void near dragborder(WINDOW wnd, int x, int y)
  765. {
  766.     RestoreBorder(dwnd.rc);
  767.     /* ------- build the dummy window -------- */
  768.     dwnd.rc.lf = x;
  769.     dwnd.rc.tp = y;
  770.     dwnd.rc.rt = dwnd.rc.lf+WindowWidth(wnd)-1;
  771.     dwnd.rc.bt = dwnd.rc.tp+WindowHeight(wnd)-1;
  772.     dwnd.ht = WindowHeight(wnd);
  773.     dwnd.wd = WindowWidth(wnd);
  774.     dwnd.parent = GetParent(wnd);
  775.     dwnd.attrib = VISIBLE | HASBORDER | NOCLIP;
  776.     InitWindowColors(&dwnd);
  777.     SaveBorder(dwnd.rc);
  778.     RepaintBorder(&dwnd, NULL);
  779. }
  780. /* ---- write the dummy window border for sizing ---- */
  781. static void near sizeborder(WINDOW wnd, int rt, int bt)
  782. {
  783.     int leftmost = GetLeft(wnd)+10;
  784.     int topmost = GetTop(wnd)+3;
  785.     int bottommost = SCREENHEIGHT-1;
  786.     int rightmost  = SCREENWIDTH-1;
  787.     if (GetParent(wnd))    {
  788.         bottommost = min(bottommost,
  789.             GetClientBottom(GetParent(wnd)));
  790.         rightmost  = min(rightmost,
  791.             GetClientRight(GetParent(wnd)));
  792.     }
  793.     rt = min(rt, rightmost);
  794.     bt = min(bt, bottommost);
  795.     rt = max(rt, leftmost);
  796.     bt = max(bt, topmost);
  797.     SendMessage(NULL, MOUSE_CURSOR, rt, bt);
  798.  
  799.     if (rt != px || bt != py)
  800.         RestoreBorder(dwnd.rc);
  801.  
  802.     /* ------- change the dummy window -------- */
  803.     dwnd.ht = bt-dwnd.rc.tp+1;
  804.     dwnd.wd = rt-dwnd.rc.lf+1;
  805.     dwnd.rc.rt = rt;
  806.     dwnd.rc.bt = bt;
  807.     if (rt != px || bt != py)    {
  808.         px = rt;
  809.         py = bt;
  810.         SaveBorder(dwnd.rc);
  811.         RepaintBorder(&dwnd, NULL);
  812.     }
  813. }
  814. #ifdef INCLUDE_MULTI_WINDOWS
  815. /* ----- adjust a rectangle to include the shadow ----- */
  816. static RECT adjShadow(WINDOW wnd)
  817. {
  818.     RECT rc;
  819.     rc = wnd->rc;
  820.     if (TestAttribute(wnd, SHADOW))    {
  821.         if (RectRight(rc) < SCREENWIDTH-1)
  822.             RectRight(rc)++;           
  823.         if (RectBottom(rc) < SCREENHEIGHT-1)
  824.             RectBottom(rc)++;
  825.     }
  826.     return rc;
  827. }
  828. /* --- repaint a rectangular subsection of a window --- */
  829. static void near PaintOverLap(WINDOW wnd, RECT rc)
  830. {
  831.     if (isVisible(wnd))    {
  832.         int isBorder, isTitle, isData;
  833.         isBorder = isTitle = FALSE;
  834.         isData = TRUE;
  835.         if (TestAttribute(wnd, HASBORDER))    {
  836.             isBorder =  RectLeft(rc) == 0 &&
  837.                         RectTop(rc) < WindowHeight(wnd);
  838.             isBorder |= RectLeft(rc) < WindowWidth(wnd) &&
  839.                         RectRight(rc) >= WindowWidth(wnd)-1 &&
  840.                         RectTop(rc) < WindowHeight(wnd);
  841.             isBorder |= RectTop(rc) == 0 &&
  842.                         RectLeft(rc) < WindowWidth(wnd);
  843.             isBorder |= RectTop(rc) < WindowHeight(wnd) &&
  844.                         RectBottom(rc) >= WindowHeight(wnd)-1 &&
  845.                         RectLeft(rc) < WindowWidth(wnd);
  846.         }
  847.         else if (TestAttribute(wnd, HASTITLEBAR))
  848.             isTitle = RectTop(rc) == 0 &&
  849.                       RectRight(rc) > 0 &&
  850.                       RectLeft(rc)<WindowWidth(wnd)-BorderAdj(wnd);
  851.  
  852.         if (RectLeft(rc) >= WindowWidth(wnd)-BorderAdj(wnd))
  853.             isData = FALSE;
  854.         if (RectTop(rc) >= WindowHeight(wnd)-BottomBorderAdj(wnd))
  855.             isData = FALSE;
  856.         if (TestAttribute(wnd, HASBORDER))    {
  857.             if (RectRight(rc) == 0)
  858.                 isData = FALSE;
  859.             if (RectBottom(rc) == 0)
  860.                 isData = FALSE;
  861.         }
  862.         if (TestAttribute(wnd, SHADOW))
  863.             isBorder |= RectRight(rc) == WindowWidth(wnd) ||
  864.                         RectBottom(rc) == WindowHeight(wnd);
  865.         if (isData)
  866.             SendMessage(wnd, PAINT, (PARAM) &rc, TRUE);
  867.         if (isBorder)
  868.             SendMessage(wnd, BORDER, (PARAM) &rc, 0);
  869.         else if (isTitle)
  870.             DisplayTitle(wnd, &rc);
  871.     }
  872. }
  873. /* ------ paint the part of a window that is overlapped
  874.             by another window that is being hidden ------- */
  875. static void PaintOver(WINDOW wnd)
  876. {
  877.     RECT wrc, rc;
  878.     wrc = adjShadow(HiddenWindow);
  879.     rc = adjShadow(wnd);
  880.     rc = subRectangle(rc, wrc);
  881.     if (ValidRect(rc))
  882.         PaintOverLap(wnd, RelativeWindowRect(wnd, rc));
  883. }
  884. /* --- paint the overlapped parts of all children --- */
  885. static void PaintOverChildren(WINDOW pwnd)
  886. {
  887.     WINDOW cwnd = FirstWindow(pwnd);
  888.     while (cwnd != NULL)    {
  889.         if (cwnd != HiddenWindow)    {
  890.             PaintOver(cwnd);
  891.             PaintOverChildren(cwnd);
  892.         }
  893.         cwnd = NextWindow(cwnd);
  894.     }
  895. }
  896. /* -- recursive overlapping paint of parents -- */
  897. static void PaintOverParents(WINDOW wnd)
  898. {
  899.     WINDOW pwnd = GetParent(wnd);
  900.     if (pwnd != NULL)    {
  901.         PaintOverParents(pwnd);
  902.         PaintOver(pwnd);
  903.         PaintOverChildren(pwnd);
  904.     }
  905. }
  906. /* - paint the parts of all windows that a window is over - */
  907. static void near PaintOverLappers(WINDOW wnd)
  908. {
  909.     HiddenWindow = wnd;
  910.     PaintOverParents(wnd);
  911. }
  912. /* --- paint those parts of a window that are overlapped --- */
  913. static void near PaintUnderLappers(WINDOW wnd)
  914. {
  915.     WINDOW hwnd = NextWindow(wnd);
  916.     while (hwnd != NULL)    {
  917.         /* ------- test only at document window level ------ */
  918.         WINDOW pwnd = GetParent(hwnd);
  919. /*        if (pwnd == NULL || GetClass(pwnd) == APPLICATION)  */  {
  920.             /* ---- don't bother testing self ----- */
  921.             if (isVisible(hwnd) && hwnd != wnd)    {
  922.                 /* --- see if other window is descendent --- */
  923.                 while (pwnd != NULL)    {
  924.                     if (pwnd == wnd)
  925.                         break;
  926.                     pwnd = GetParent(pwnd);
  927.                 }
  928.                 /* ----- don't test descendent overlaps ----- */
  929.                 if (pwnd == NULL)    {
  930.                     /* -- see if other window is ancestor --- */
  931.                     pwnd = GetParent(wnd);
  932.                     while (pwnd != NULL)    {
  933.                         if (pwnd == hwnd)
  934.                             break;
  935.                         pwnd = GetParent(pwnd);
  936.                     }
  937.                     /* --- don't test ancestor overlaps --- */
  938.                     if (pwnd == NULL)    {
  939.                         HiddenWindow = GetAncestor(hwnd);
  940.                         ClearVisible(HiddenWindow);
  941.                         PaintOver(wnd);
  942.                         SetVisible(HiddenWindow);
  943.                     }
  944.                 }
  945.             }
  946.         }
  947.         hwnd = NextWindow(hwnd);
  948.     }
  949.     /* --------- repaint all children of this window
  950.         the same way ----------- */
  951.     hwnd = FirstWindow(wnd);
  952.     while (hwnd != NULL)    {
  953.         PaintUnderLappers(hwnd);
  954.         hwnd = NextWindow(hwnd);
  955.     }
  956. }
  957. #endif /* #ifdef INCLUDE_MULTI_WINDOWS */
  958.  
  959. /* --- save video area to be used by dummy window border --- */
  960. static void SaveBorder(RECT rc)
  961. {
  962.     RECT lrc;
  963.     int i;
  964.     int *cp;
  965.     Bht = RectBottom(rc) - RectTop(rc) + 1;
  966.     Bwd = RectRight(rc) - RectLeft(rc) + 1;
  967.     Bsave = DFrealloc(Bsave, (Bht + Bwd) * 4);
  968.  
  969.     lrc = rc;
  970.     RectBottom(lrc) = RectTop(lrc);
  971.     getvideo(lrc, Bsave);
  972.     RectTop(lrc) = RectBottom(lrc) = RectBottom(rc);
  973.     getvideo(lrc, Bsave + Bwd);
  974.     cp = Bsave + Bwd * 2;
  975.     for (i = 1; i < Bht-1; i++)    {
  976.         *cp++ = GetVideoChar(RectLeft(rc),RectTop(rc)+i);
  977.         *cp++ = GetVideoChar(RectRight(rc),RectTop(rc)+i);
  978.     }
  979. }
  980. /* ---- restore video area used by dummy window border ---- */
  981. static void RestoreBorder(RECT rc)
  982. {
  983.     if (Bsave != NULL)    {
  984.         RECT lrc;
  985.         int i;
  986.         int *cp;
  987.         lrc = rc;
  988.         RectBottom(lrc) = RectTop(lrc);
  989.         storevideo(lrc, Bsave);
  990.         RectTop(lrc) = RectBottom(lrc) = RectBottom(rc);
  991.         storevideo(lrc, Bsave + Bwd);
  992.         cp = Bsave + Bwd * 2;
  993.         for (i = 1; i < Bht-1; i++)    {
  994.             PutVideoChar(RectLeft(rc),RectTop(rc)+i, *cp++);
  995.             PutVideoChar(RectRight(rc),RectTop(rc)+i, *cp++);
  996.         }
  997.         free(Bsave);
  998.         Bsave = NULL;
  999.     }
  1000. }
  1001. /* ----- test if screen coordinates are in a window ---- */
  1002. static BOOL InsideWindow(WINDOW wnd, int x, int y)
  1003. {
  1004.     RECT rc;
  1005.     rc = WindowRect(wnd);
  1006.     if (!TestAttribute(wnd, NOCLIP))    {
  1007.         WINDOW pwnd = GetParent(wnd);
  1008.         while (pwnd != NULL)    {
  1009.             rc = subRectangle(rc, ClientRect(pwnd));
  1010.             pwnd = GetParent(pwnd);
  1011.         }
  1012.     }
  1013.     return InsideRect(x, y, rc);
  1014. }
  1015.  
  1016. BOOL isDerivedFrom(WINDOW wnd, CLASS class)
  1017. {
  1018.     CLASS tclass = GetClass(wnd);
  1019.     while (tclass != -1)    {
  1020.         if (tclass == class)
  1021.             return TRUE;
  1022.         tclass = (classdefs[tclass].base);
  1023.     }
  1024.     return FALSE;
  1025. }
  1026.  
  1027. /* -- find the oldest document window ancestor of a window -- */
  1028. WINDOW GetAncestor(WINDOW wnd)
  1029. {
  1030.     if (wnd != NULL)    {
  1031.         while (GetParent(wnd) != NULL)    {
  1032.             if (GetClass(GetParent(wnd)) == APPLICATION)
  1033.                 break;
  1034.             wnd = GetParent(wnd);
  1035.         }
  1036.     }
  1037.     return wnd;
  1038. }
  1039.  
  1040. BOOL isVisible(WINDOW wnd)
  1041. {
  1042.     while (wnd != NULL)    {
  1043.         if (isHidden(wnd))
  1044.             return FALSE;
  1045.         wnd = GetParent(wnd);
  1046.     }
  1047.     return TRUE;
  1048. }
  1049.  
  1050. /* -- adjust a window's rectangle to clip it to its parent - */
  1051. static RECT near ClipRect(WINDOW wnd)
  1052. {
  1053.     RECT rc;
  1054.     rc = WindowRect(wnd);
  1055.     if (TestAttribute(wnd, SHADOW))    {
  1056.         RectBottom(rc)++;
  1057.         RectRight(rc)++;
  1058.     }
  1059.     return ClipRectangle(wnd, rc);
  1060. }
  1061.  
  1062. /* -- get the video memory that is to be used by a window -- */
  1063. static void GetVideoBuffer(WINDOW wnd)
  1064. {
  1065.     RECT rc;
  1066.     int ht;
  1067.     int wd;
  1068.  
  1069.     rc = ClipRect(wnd);
  1070.     ht = RectBottom(rc) - RectTop(rc) + 1;
  1071.     wd = RectRight(rc) - RectLeft(rc) + 1;
  1072.     wnd->videosave = DFrealloc(wnd->videosave, (ht * wd * 2));
  1073.     get_videomode();
  1074.     getvideo(rc, wnd->videosave);
  1075. }
  1076.  
  1077. /* -- put the video memory that is used by a window -- */
  1078. static void PutVideoBuffer(WINDOW wnd)
  1079. {
  1080.     if (wnd->videosave != NULL)    {
  1081.         RECT rc;
  1082.         rc = ClipRect(wnd);
  1083.         get_videomode();
  1084.         storevideo(rc, wnd->videosave);
  1085.         free(wnd->videosave);
  1086.         wnd->videosave = NULL;
  1087.     }
  1088. }
  1089.  
  1090. /* ------- return TRUE if awnd is an ancestor of wnd ------- */
  1091. BOOL isAncestor(WINDOW wnd, WINDOW awnd)
  1092. {
  1093.     while (wnd != NULL)    {
  1094.         if (wnd == awnd)
  1095.             return TRUE;
  1096.         wnd = GetParent(wnd);
  1097.     }
  1098.     return FALSE;
  1099. }
  1100.  
  1101.  
  1102.  
  1103. 
  1104.